package com.heu.blood.charge.controller;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

import com.heu.blood.application.exception.ApplicationExceptionEnum;
import com.heu.blood.charge.exception.ChargeExceptionEnum;
import com.heu.blood.charge.vo.BloodChargeEntityVo;
import com.heu.blood.charge.vo.BloodChargeHistoryUploadVo;
import com.heu.blood.common.aop.ControllerLogAnnotation;
import com.heu.blood.common.enums.BloodChargeEnum;
import com.heu.blood.globalExceptionHandler.MyException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.heu.blood.charge.entity.BloodChargeEntity;
import com.heu.blood.charge.service.BloodChargeService;
import com.heu.blood.common.utils.PageUtils;
import com.heu.blood.common.utils.R;



/**
 * 
 *
 * @author lhh
 * @email lhh1612325771@163.com
 * @date 2024-07-19 17:04:18
 */
@RestController
@RequestMapping("charge/bloodcharge")
public class BloodChargeController {
    @Autowired
    private BloodChargeService bloodChargeService;

    /**
     * 列表
     */
    @RequestMapping("/list")
    @ControllerLogAnnotation(module = "收费模块", operate = "展示所有收费信息")
    public R list(BloodChargeEntityVo bloodChargeEntityVo){
        PageUtils page = bloodChargeService.queryPage(bloodChargeEntityVo);

        return R.ok().put("page", page);
    }



    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
    @ControllerLogAnnotation(module = "收费模块", operate = "根据id查询收费信息")
    public R info(@PathVariable("id") Integer id){
		BloodChargeEntity bloodCharge = bloodChargeService.getById(id);

        return R.ok().put("bloodCharge", bloodCharge);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    @ControllerLogAnnotation(module = "收费模块", operate = "保存收费信息")
    public R save(@RequestBody BloodChargeEntity bloodCharge){
		bloodChargeService.saveCharge(bloodCharge);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    @ControllerLogAnnotation(module = "收费模块", operate = "修改收费信息")
    public R update(@RequestBody BloodChargeEntity bloodCharge){
		bloodChargeService.updateById(bloodCharge);

        return R.ok();
    }

    /**
     * 修改收费的状态功能UNVERIFIED->VERIFIED
     */
    @RequestMapping("/updateState")
    @ControllerLogAnnotation(module = "收费模块", operate = "修改收费的状态功能UNVERIFIED->VERIFIED")
    public R update(@RequestBody List<BloodChargeEntity> bloodCharge){
        for (BloodChargeEntity Entity: bloodCharge) {
            Entity.setChargeState(BloodChargeEnum.CHARGE_VERIFIED.code());
        }
        bloodChargeService.updateBatchById(bloodCharge);

        return R.ok();
    }
    /**
     * 修改收费的状态功能UNVERIFIED->VERIFIED、并设置核验结果SUCCESS
     */
    @RequestMapping("/verifySuccess")
    @ControllerLogAnnotation(module = "收费模块", operate = "收费核验成功")
    public R verifySuccess(@RequestBody List<BloodChargeEntity> bloodCharge){
        int count = bloodChargeService.verifySuccess(bloodCharge);
        return R.ok().put("updatedCount", count);
    }
    /**
     * 修改收费的状态功能UNVERIFIED->VERIFIED、并设置核验结果FAILURE
     */
    @RequestMapping("/verifyFailure")
    @ControllerLogAnnotation(module = "收费模块", operate = "收费核验失败")
    public R verifyFailure(@RequestBody List<BloodChargeEntity> bloodCharge){
        int count = bloodChargeService.verifyFailure(bloodCharge);
        return R.ok().put("updatedCount", count);
    }


    /**
     * 删除
     */
    @RequestMapping("/delete")
    @ControllerLogAnnotation(module = "收费模块", operate = "删除收费信息")
    public R delete(@RequestBody Integer[] ids){
		bloodChargeService.removeByIds(Arrays.asList(ids));

        return R.ok();
    }

    @RequestMapping("/upload")
    @ControllerLogAnnotation(module = "收费模块", operate = "收费信息上传his系统并记录推送信息")
    public R upload(@RequestBody BloodChargeHistoryUploadVo bloodChargeHistoryUploadVo){
        int i = bloodChargeService.upload(bloodChargeHistoryUploadVo);
        if (i == 1) {
            return R.ok();
        } else if (i == 0) {
            throw new MyException(ChargeExceptionEnum.JSON_FAILED);
        } else if(i == 2) {
            throw new MyException(ChargeExceptionEnum.SAVE_FAILED);
        } else if(i == 3) {
            throw new MyException(ChargeExceptionEnum.IDS_NULL);
        } else if(i == 4) {
            throw new MyException(ChargeExceptionEnum.IDS_STATE_WRONG);
        }
        return R.ok();
    }

    @RequestMapping("/saveBloodCharge")
    @ControllerLogAnnotation(module = "收费模块", operate = "保存收费信息")
    public R saveBloodCharge(BloodChargeEntityVo bloodChargeEntityVo){
        int i = bloodChargeService.saveBloodCharge(bloodChargeEntityVo);
        if (i == 0) {
            return R.ok();
        } else if(i == 1) {
            throw new MyException(ChargeExceptionEnum.ILLEGAL_PARAMS);
        }
        return R.ok();
    }

}
